home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / SetPixelsQuickly.c < prev    next >
Text File  |  1995-08-14  |  24KB  |  722 lines

  1. /*
  2. SetPixelsQuickly.c
  3. All the Set... routines poke a row of pixels.
  4. All the Get... routines peek a row of pixels.
  5. SetPixelsQuickly and GetPixelsQuickly use the current port.
  6. SetWindowPixelsQuickly and GetWindowPixelsQuickly use the supplied window.
  7. SetDevicePixelsQuickly and GetDevicePixelsQuickly use the supplied video device.
  8. SetPixmapPixelsQuickly and GetPixmapPixelsQuickly use the supplied pix/bitmap.
  9.  
  10. int SetPixelsQuickly(int x,int y,unsigned long value[],short n);
  11. int GetPixelsQuickly(int x,int y,unsigned long value[],short n);
  12. int SetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long value[],short n);
  13. int GetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long value[],short n);
  14. int SetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n);
  15. int GetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n);
  16. int SetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[],short n);
  17. int GetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[],short n);
  18.  
  19. This is the fastest and easiest way to transform back and forth between a PixMap
  20. or BitMap and a numerical representation of an arbitrary image amenable to
  21. computation in C. You can think of these routines as replacements for the
  22. official Apple SetCPixel, etc. Their virtue is that they're very fast and don't
  23. translate your index via any color table. Set... and Get... ignore the color
  24. spec arrays, giving you direct access to the unsigned number stored in each
  25. pixel (whether 1, 2, 4, 8, 16, or 32 bits). With appropriate casting you may
  26. supply a CWindowPtr in place of the WindowPtr, and you may supply a BitMapPtr in
  27. place of the PixMapPtr.
  28.  
  29. For all the routines, you supply the (x,y) coordinate of the first pixel you
  30. want to access in the appropriate coordinate system, an unsigned long array
  31. called "value", and the number of pixels to access. x will increment for each
  32. successive pixel. Set... will copy values, one by one, from "value" into the
  33. pix/bitmap. Get... will copy from the pix/bitmap into "value". The routines work
  34. with all pixel sizes: 1 to 32 bits. The "value" array that you supply is 
  35. unsigned long.
  36.  
  37. All the routines clip to enforce either the pix/bitmap's bounds, or the window's
  38. portRect (if you pass a window or use the current window/port). The only way of
  39. getting into trouble would be to pass a window's PixMap directly. It is
  40. difficult to figure out the clipping of a window's PixMap; RectToAddress does
  41. it, but only on the first call, after which we use the old cached answer, which
  42. doesn't include clipping information.
  43.  
  44. The returned value of the function is normally zero, indicating success, but
  45. will be nonzero if the request could not be fully satisfied. All the routines
  46. clip to the bit/pixmap bounds; this is treated as normal by the Set... routines
  47. and is reported as an error by the Get... routines. In the latter case some of
  48. the elements of the value array will have been left untouched because the pixels
  49. they correspond to could not be accessed.
  50.  
  51. These routines (and RectToAddress) do not "move memory", i.e. they don't give
  52. the Memory Manager any pretext for shuffling around the memory allocations and
  53. changing its master pointers. That's why it's OK to dereference the pixmap handle,
  54. passing as an argument a temporary copy of the pixmap's master pointer.
  55.  
  56. SetPixmapPixelsQuickly and GetPixmapPixelsQuickly run fast by caching the
  57. information that they get about your Bit/Pixmap from RectToAddress. Set.. and
  58. Get.. each have their own cache. The cache is assumed to be fresh (i.e. is not
  59. recomputed) if it receives the same Pix/Bitmap as last time. It checks whether
  60. the pix/bitmap has the same address, baseAddr, rowBytes, and bounds. However, if
  61. the pix/bitmap has the same baseAddr as the mainDevice, yet actually corresponds
  62. to another device, then the cache is not used, because in a multi-screen
  63. environment QuickDraw associates all windows' pixmaps with the baseAddr of the
  64. main device (to create the illusion of a continuous desktop extending across
  65. multiple screens). You can force a flush of the cache by passing a NULL
  66. bit/pixmap pointer. (This will result in a returned value of 0, since the flush
  67. is always successful.)
  68.  
  69. See the demo Grating.c for an example of how to use this to display a pattern on
  70. the screen.
  71.  
  72.     // This is a simple write-then-read test of these routines,
  73.     // writing random numbers to the top line of the main screen.
  74.     unsigned long row[100],row2[100];
  75.     int rowLength=100,clutSize,i;
  76.     device=GetMainDevice();
  77.     clutSize=GDClutSize(device);
  78.     for(i=0;i<rowLength;i++)row[i]=nrand(clutSize);
  79.     SetDevicePixelsQuickly(device,0,0,row,rowLength);
  80.     GetDevicePixelsQuickly(device,0,0,row2,rowLength);
  81.     for(i=0;i<rowLength;i++)if(row2[i]!=row[i])
  82.         printf("%d-th pixel: wrote %ld, but read %ld\n",i,row[i],row2[i]);
  83.  
  84. HISTORY:
  85. 4/4/89     dgp wrote SetIPixel.c
  86. 9/8/90     dgp updated to work with 32 bit QuickDraw, if present.
  87. 10/15/90 bf renamed to SetIPixelGW.c and modified for drawing to off screen pix maps.
  88. 4/26/92     dgp Merged the two variants: SetIPixel.c and SetIPixelGW.c to produce the
  89.             new file SetOnePixel.c. Renamed existing routines to SetPixmapPixel,
  90.             GetPixmapPixel,SetDevicePixel,GetDevicePixel. Added SetOnePixel and 
  91.             GetOnePixel. Generalized to handle any pixelSize, and accept bitmaps as well
  92.             as pixmaps.
  93. 12/23/92 dgp Doubled the speed of SetPixmapPixel and GetPixmapPixel (and thus sped up
  94.             all the routines that call them) by caching the answers from RectToAddress. 
  95. 1/6/93 dgp    Fixed tiny but disastrous bug in GetPixmapPixel (wasn't saving old x and y).
  96. 1/22/93    dgp    Check more PixMap fields to make sure cache is not stale.
  97. 2/7/93    dgp Wrote SetPixelsQuickly.c
  98. 2/8/93    dgp Ironed out some wrinkles in the clipping.
  99. 4/27/93    dgp    Invalidate cache if baseAddr==mainDeviceBaseAddr.
  100. 6/4/93     dhb Modified to deal with MEX file weirdness in 24-bit mode.
  101.             These are all conditionally compiled in with the MATLAB
  102.             symbol. No intentional changes to original.
  103.         dgp    Here's my attempt to explain the problem and solution. 
  104.             MATLAB puts garbage in the high byte of the program counter register, 
  105.             so the machine crashes the instant you switch into 32-bit mode. 
  106.             The fix is to call a subroutine whose address has been cleaned by calling
  107.             StripAddress. This puts a 32-bit-clean address into the program counter,
  108.             until the subroutine returns. 
  109.         dhb The SwapMMUMode calls are arranged so that when MATLAB is defined only
  110.             a minimal amount of code is run in 32-bit mode.  The problem is that 
  111.             register A4, which is used in global addressing, also has garbage in
  112.             the high byte.  Not only is A4 used to reference globals, but as far
  113.             as I could tell, it is also used as an offset in certain code jumps. 
  114.             So if it is wrong, all hell breaks loose.  The solution is to run only
  115.             the code that blits the image in 32-bit mode.  I expect that this problem
  116.             will be fixed by a future version of MATLAB.
  117.         dgp    THINK C 5 sometimes implemented "switch" by a subroutine call, that might
  118.             explain the need to put the SwapMMUMode calls inside the switch, since
  119.             any subroutine call will fail when the global address register contains
  120.             garbage.
  121. 6/23/93    dgp Used THINK C preprocessor and MPW Compare to confirm that
  122.             code is unchanged when MATLAB is false.
  123. 7/9/93    dgp    Replaced calls to QD32Exists() by the variable can32, which is quicker,
  124.             and based on a more appropriate test: gestalt32BitCapable.
  125.             Test MATLAB in if() instead of #if. This is easier to read and
  126.             the compiler is smart enough to evaluate it at compile time so there's 
  127.             no runtime penalty.
  128. 4/11/94    dgp    In response to request from David Brainard, don't flush cache
  129.             when the pixmap truly refers to the main screen. Pixmaps for
  130.             screens other than the main screen still require a flush and call 
  131.             to RectToAddress in order to clip by the appropriate device bounds.
  132.             This wouldn't be necessary if we retained the old device or its
  133.             bounds, but unfortunately RectToAddress doesn't return them.
  134. 6/14/94    dgp    Discovered that TimeVideo crashed on Mac II with dirty ROMs. The
  135.             problem is that SetPixelsQuickly.c was naively
  136.             using the gestalt32BitCapable
  137.             bit to decide whether SwapMMUMode() was available, and this test
  138.             returns false when the ROMs are dirty (enabling MODE32 causes
  139.             the test to return true). Thus we were blithely skipping the
  140.             call to SwapMMUMode and trying to access 32-bit video addresses
  141.             in 24-bit mode. The correct test is to call
  142.             TrapAvailable(_SwapMMUMode). The test is now refined to call
  143.             SwapMMUMode only if we really need to.
  144. 10/26/94 dgp Changed GetWindowPixels and SetWindowPixels to use GetBitMapPtr, to
  145.             deal correctly with GWorldPtr, i.e. lock the pixels.
  146. 5/23/95 dgp Apple changed the prototype in the header file from SwapMMUMode(char *) to 
  147.             SwapMMUMode(signed char *). To retain compatibility with both old and new
  148.             headers, I cast the argument (void *).
  149. 6/7/95    dhb, gmb Weird MATLAB kluge only necessary if GENERATING68K.
  150. */
  151. #include "VideoToolbox.h"
  152. #ifndef __TRAPS__
  153.     #include <Traps.h>        // _SwapMMUMode
  154. #endif
  155. #define USE_CACHE 1    // set to zero to disable cache
  156. void ReadPixels(int x,int y,int n,unsigned long *value
  157.     ,unsigned char *baseAddr,long pixelSize,long rowBytes);
  158. void WritePixels(int x,int y,int n,unsigned long *value
  159.     ,unsigned char *baseAddr,long pixelSize,long rowBytes);
  160.  
  161. int SetPixelsQuickly(int x,int y,unsigned long value[],short n)
  162. // (x,y) is in the local coordinate system of the current port.
  163. {
  164.     WindowPtr window;
  165.  
  166.     GetPort(&window);
  167.     return SetWindowPixelsQuickly(window,x,y,value,n);
  168. }
  169.  
  170. int GetPixelsQuickly(int x,int y,unsigned long value[],short n)
  171. // (x,y) is in the local coordinate system of the current port.
  172. {
  173.     WindowPtr window;
  174.  
  175.     GetPort(&window);
  176.     return GetWindowPixelsQuickly(window,x,y,value,n);
  177. }
  178.  
  179. int SetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long *value,short n)
  180. // (x,y) is in the local coordinate system of the window.
  181. // Accepts either WindowPtr or CWindowPtr.
  182. {
  183.     Rect r;
  184.     
  185.     if(window==NULL)return 1;
  186.     
  187.     // Clip to portRect.
  188.     SetRect(&r,x,y,x+n,y+1);
  189.     if(!SectRect(&window->portRect,&r,&r))return 0;
  190.     value+=r.left-x;
  191.     x=r.left;
  192.     n=r.right-r.left;
  193.     
  194.     return SetPixmapPixelsQuickly((PixMapPtr)GetBitMapPtr((CWindowPtr)window),x,y,value,n);
  195. }
  196.  
  197. int GetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long *value,short n)
  198. // (x,y) is in the local coordinate system of the window.
  199. // Accepts WindowPtr, CWindowPtr, or GWorldPtr.
  200. {
  201.     int error=0;
  202.     Rect r;
  203.  
  204.     if(window==NULL)return 1;
  205.     
  206.     // Clip to portRect.
  207.     SetRect(&r,x,y,x+n,y+1);
  208.     if(!SectRect(&window->portRect,&r,&r))return 1;
  209.     if(x!=r.left || x+n!=r.right){    // Update after clipping.
  210.         error=1;
  211.         value+=r.left-x;
  212.         x=r.left;
  213.         n=r.right-r.left;
  214.     }
  215.     
  216.     error|=GetPixmapPixelsQuickly((PixMapPtr)GetBitMapPtr((CWindowPtr)window),x,y,value,n);
  217.     return error;
  218. }
  219.  
  220. int SetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n)
  221. // (x,y) is relative to the upper left hand corner of the screen.
  222. {
  223.     Rect r;
  224.     
  225.     if(device==NULL)return 1;
  226.     x+=(*(*device)->gdPMap)->bounds.left;
  227.     y+=(*(*device)->gdPMap)->bounds.top;
  228.  
  229.     // Clip to device bounds.
  230.     SetRect(&r,x,y,x+n,y+1);
  231.     if(!SectRect(&(*(*device)->gdPMap)->bounds,&r,&r))return 0;
  232.     value+=r.left-x;    // Update after clipping.
  233.     x=r.left;
  234.     n=r.right-r.left;
  235.     
  236.     return SetPixmapPixelsQuickly(*(*device)->gdPMap,x,y,value,n);
  237. }
  238.  
  239. int GetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n)
  240. // (x,y) is relative to the upper left hand corner of the screen.
  241. {
  242.     int error=1;
  243.     Rect r;
  244.     
  245.     if(device==NULL)return 1;
  246.     x+=(*(*device)->gdPMap)->bounds.left;
  247.     y+=(*(*device)->gdPMap)->bounds.top;
  248.     
  249.     // Clip to device bounds.
  250.     SetRect(&r,x,y,x+n,y+1);
  251.     if(!SectRect(&(*(*device)->gdPMap)->bounds,&r,&r))return 1;
  252.     if(x!=r.left || x+n!=r.right){    // Update after clipping.
  253.         error=1;
  254.         value+=r.left-x;
  255.         x=r.left;
  256.         n=r.right-r.left;
  257.     }
  258.     error|=GetPixmapPixelsQuickly(*(*device)->gdPMap,x,y,value,n);
  259.     return error;
  260. }
  261.  
  262. #if MATLAB && GENERATING68K
  263. int SetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  264.     ,register short n)
  265. {
  266.   int (*goSet) (PixMapPtr pmPtr,int x,int y,unsigned long value[],register short n);
  267.   int SPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  268.       ,register short n);
  269.  
  270.   goSet = (void *) StripAddress(&SPixmapPixelsQuickly);
  271.   return( (*goSet) (pmPtr,x,y,value,n) );
  272. }
  273. static int SPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  274.     ,register short n)
  275. #else
  276. int SetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  277.     ,register short n)
  278. #endif
  279. // Pokes a row of pixels. Accepts either pixmap or bitmap pointer.
  280. // (x,y) is in the coordinate system of the bit/pixmap.
  281. // Speed is enhanced by reusing the cached information from last time if it's the
  282. // same Pix/Bitmap as last time, i.e. same address, baseAddr, rowBytes, and bounds.
  283. // You can flush this cache by passing a NULL bit/pixmap pointer.
  284. {
  285.     static PixMapPtr oldPmPtr=(PixMapPtr)-1;
  286.     static int oldX,oldY;
  287.     static short rowBytes,logPixelSize,bitsOffset;
  288.     static unsigned char *pixelPtr;
  289.     static BitMap oldMap;
  290.     static Ptr mainBaseAddr=NULL;
  291.     static Boolean is32,can32,need32,firstTime=1;
  292.     signed char mode32=true32b;
  293.     int error=0;
  294.     Rect r;
  295.     long gestalt;
  296.     Boolean otherScreen;    // masquerading as extension of main screen
  297.     short pixelSize;
  298.     
  299.     if(firstTime){
  300.         Gestalt(gestaltAddressingModeAttr,&gestalt);
  301.         is32=gestalt&(1L<<gestalt32BitAddressing);
  302.         can32=TrapAvailable(_SwapMMUMode);
  303.         firstTime=0;
  304.     }
  305.     if(pmPtr==NULL){
  306.         oldPmPtr=(PixMapPtr)-1;    // invalidate cache
  307.         return 0;
  308.     }
  309.     // Clip to pix/bitmap bounds.
  310.     SetRect(&r,x,y,x+n,y+1);
  311.     if(mainBaseAddr==NULL){
  312.         if(QD8Exists())mainBaseAddr=(*(*GetMainDevice())->gdPMap)->baseAddr;
  313.         else mainBaseAddr=(void *)-1;
  314.     }
  315.     otherScreen= pmPtr->baseAddr==mainBaseAddr 
  316.         && (pmPtr->bounds.top!=0 || pmPtr->bounds.left!=0);
  317.     // Clip unless it's other screen.
  318.     if(!otherScreen)
  319.         if(!SectRect(&pmPtr->bounds,&r,&r))return 0;    // go home if we're done
  320.     if(!USE_CACHE
  321.         || otherScreen
  322.         || pmPtr!=oldPmPtr
  323.         || pmPtr->baseAddr!=oldMap.baseAddr 
  324.         || pmPtr->rowBytes!=oldMap.rowBytes 
  325.         || *(long *)&pmPtr->bounds.top!=*(long *)&oldMap.bounds.top
  326.         || *(long *)&pmPtr->bounds.bottom!=*(long *)&oldMap.bounds.bottom){
  327.         // Cache is stale. Get fresh values.
  328.         long value=0;
  329.         
  330.         // RectToAddress computes pixelPtr and clips r to the bit/pixmap bounds.
  331.         pixelPtr=RectToAddress(pmPtr,&r,&rowBytes,&pixelSize,&bitsOffset);
  332.         if(pixelPtr==NULL){
  333.             oldPmPtr=(PixMapPtr)-1;    // invalidate cache
  334.             return 0;
  335.         }
  336.         oldPmPtr=pmPtr;
  337.         oldMap=*(BitMap *)pmPtr;
  338.         logPixelSize=Log2L(pixelSize);
  339.         value+=r.left-x;    // Update after clipping.
  340.         x=r.left;
  341.         n=r.right-r.left;
  342.     }else{
  343.         // Cache is fresh. Merely correct for changes in x and y.
  344.         if(pixelPtr==NULL)return 1;
  345.         value+=r.left-x;    // Update after clipping.
  346.         x=r.left;
  347.         n=r.right-r.left;
  348.         if(x!=oldX){
  349.             if(logPixelSize<3){
  350.                 register long bits;
  351.                 bits=bitsOffset+(long)(x-oldX)<<logPixelSize;
  352.                 pixelPtr+=bits>>3;
  353.                 bitsOffset=bits&7;
  354.             }else pixelPtr+=(x-oldX)<<(logPixelSize-3);
  355.         }
  356.         if(y!=oldY)pixelPtr+=(long)(y-oldY)*rowBytes;
  357.     }
  358.     pixelSize=1<<logPixelSize;
  359.     WritePixels(bitsOffset/pixelSize,0,n,value,pixelPtr,pixelSize,rowBytes);
  360.     oldX=x;
  361.     oldY=y;
  362.     return error;
  363. }
  364.  
  365. #if MATLAB && GENERATING68K
  366. int GetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  367.     ,register short n)
  368. {
  369.   int (*goSet) (PixMapPtr pmPtr,int x,int y,unsigned long value[],register short n);
  370.   int GPixmapPixelsQuickly (PixMapPtr pmPtr,int x,int y,unsigned long value[],register short n);
  371.  
  372.   goSet = (void *) StripAddress(&GPixmapPixelsQuickly);
  373.   return( (*goSet) (pmPtr,x,y,value,n) );
  374. }
  375. static int GPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  376.     ,register short n)
  377. #else
  378. int GetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  379.     ,register short n)
  380. #endif
  381. // Peeks a rows of pixels of any size. Accepts either pixmap or bitmap pointer.
  382. // (x,y) is in the coordinate system of the bit/pixmap.
  383. // Speed is enhanced by reusing the cached information from last time if it's the
  384. // same Pix/Bitmap as last time, i.e. same address, baseAddr,rowBytes, and bounds.
  385. // You can force it to flush its cache by passing a NULL bit/pixmap pointer.
  386. {
  387.     static PixMapPtr oldPmPtr=(PixMapPtr)-1;
  388.     static int oldX,oldY;
  389.     static short rowBytes,logPixelSize,bitsOffset;
  390.     static unsigned char *pixelPtr;
  391.     static BitMap oldMap;
  392.     static Ptr mainBaseAddr=NULL;
  393.     int error=0;
  394.     Rect r;
  395.     Boolean otherScreen;    // masquerading as extension of main screen
  396.     short pixelSize;
  397.     
  398.     if(pmPtr==NULL){
  399.         oldPmPtr=(PixMapPtr)-1;    // invalidate cache
  400.         return 0;
  401.     }
  402.     // Clip to pix/bitmap bounds.
  403.     SetRect(&r,x,y,x+n,y+1);
  404.     if(mainBaseAddr==NULL){
  405.         if(QD8Exists())mainBaseAddr=(*(*GetMainDevice())->gdPMap)->baseAddr;
  406.         else mainBaseAddr=(void *)-1;
  407.     }
  408.     otherScreen= pmPtr->baseAddr==mainBaseAddr 
  409.         && (pmPtr->bounds.top!=0 || pmPtr->bounds.left!=0);
  410.     // Clip unless it's other screen.
  411.     if(!otherScreen)
  412.         if(!SectRect(&pmPtr->bounds,&r,&r))return 1;    // go home if we're done
  413.     if(!USE_CACHE
  414.         || otherScreen
  415.         || pmPtr!=oldPmPtr
  416.         || pmPtr->baseAddr!=oldMap.baseAddr 
  417.         || pmPtr->rowBytes!=oldMap.rowBytes 
  418.         || *(long *)&pmPtr->bounds.top!=*(long *)&oldMap.bounds.top
  419.         || *(long *)&pmPtr->bounds.bottom!=*(long *)&oldMap.bounds.bottom){
  420.         // Cache is stale. Get fresh values.
  421.         long value=0;
  422.  
  423.         // RectToAddress computes pixelPtr and clips r to the bit/pixmap bounds.
  424.         pixelPtr=RectToAddress(pmPtr,&r,&rowBytes,&pixelSize,&bitsOffset);
  425.         if(pixelPtr==NULL){
  426.             oldPmPtr=(PixMapPtr)-1;    // invalidate cache
  427.             return 0;
  428.         }
  429.         oldPmPtr=pmPtr;
  430.         oldMap=*(BitMap *)pmPtr;
  431.         logPixelSize=Log2L(pixelSize);
  432.         if(x!=r.left || x+n!=r.right){    // Update after clipping.
  433.             error=1;
  434.             value+=r.left-x;
  435.             x=r.left;
  436.             n=r.right-r.left;
  437.         }
  438.     }else{
  439.         // Cache is fresh. Merely correct for changes in x and y.
  440.         if(pixelPtr==NULL)return 1;
  441.         if(x!=r.left || x+n!=r.right){    // Update after clipping.
  442.             error=1;
  443.             value+=r.left-x;
  444.             x=r.left;
  445.             n=r.right-r.left;
  446.         }
  447.         if(x!=oldX){
  448.             if(logPixelSize<3){
  449.                 register long bits;
  450.                 bits=bitsOffset+(long)(x-oldX)<<logPixelSize;
  451.                 pixelPtr+=bits>>3;
  452.                 bitsOffset=bits&7;
  453.             }else pixelPtr+=(x-oldX)<<(logPixelSize-3);
  454.         }
  455.         if(y!=oldY)pixelPtr+=(long)(y-oldY)*rowBytes;
  456.     }
  457.     pixelSize=1<<logPixelSize;
  458.     ReadPixels(bitsOffset/pixelSize,0,n,value,pixelPtr,pixelSize,rowBytes);
  459.     oldX=x;
  460.     oldY=y;
  461.     return error;
  462. }
  463.  
  464. // Fast access to pixels, with no overhead
  465. void WritePixels(int x,int y,int n,unsigned long *value
  466.     ,unsigned char *baseAddr,long pixelSize,long rowBytes)
  467. {
  468.     static Boolean can32,is32,firstTime=1;
  469.     Boolean need32;
  470.     signed char mode32=true32b;
  471.     short bitsOffset=0;
  472.     unsigned char *pixelPtr=baseAddr;
  473.     int shift;
  474.  
  475.     if(firstTime){
  476.         long gestalt;
  477.         can32=TrapAvailable(_SwapMMUMode);
  478.         Gestalt(gestaltAddressingModeAttr,&gestalt);
  479.         is32=gestalt&(1L<<gestalt32BitAddressing);
  480.         firstTime=0;
  481.     }
  482.     if(x!=0){
  483.             register long bits;
  484.  
  485.             bits=x*pixelSize;
  486.             pixelPtr+=bits>>3;
  487.             bitsOffset=bits&7;
  488.     }
  489.     if(y!=0)pixelPtr+=y*rowBytes;
  490.     need32=(unsigned long)pixelPtr>0xffffffUL;
  491.     if(need32 && !can32)PrintfExit("%s line %d: 32 bit address in 24 bit computer.\n"
  492.         ,__FILE__,__LINE__);
  493.     need32=need32 && !is32;
  494.     switch(pixelSize){
  495.     case 1:{
  496.         register unsigned char val,mask,*ptr=pixelPtr;
  497.         shift=sizeof(*ptr)*8;
  498.         shift-=bitsOffset;    // from right, instead of from left
  499.         if(need32)SwapMMUMode((void *)&mode32);
  500.         do{
  501.             val=mask=0;
  502.             do{
  503.                 shift-=1;
  504.                 val<<=1;
  505.                 mask<<=1;
  506.                 if(n>0){
  507.                     val|=(*value++)&1;
  508.                     mask|=1;
  509.                     n--;
  510.                 }else{
  511.                     val<<=shift;
  512.                     mask<<=shift;
  513.                     break;
  514.                 }
  515.             }while(shift>0);
  516.             mask=~mask;
  517.             *ptr= *ptr & mask | (unsigned char)val;
  518.             ptr++;
  519.             shift=sizeof(*ptr)*8;
  520.         }while(n>0);
  521.         if(need32)SwapMMUMode((void *)&mode32);
  522.         break;
  523.     }
  524.     case 2:{
  525.         register unsigned char val,mask,*ptr=pixelPtr;
  526.         shift=sizeof(*ptr)*8;
  527.         shift-=bitsOffset;    // from right, instead of from left
  528.         if(need32)SwapMMUMode((void *)&mode32);
  529.         do{
  530.             val=mask=0;
  531.             do{
  532.                 shift-=2;
  533.                 val<<=2;
  534.                 mask<<=2;
  535.                 if(n>0){
  536.                     val|=(*value++)&3;
  537.                     mask|=3;
  538.                     n--;
  539.                 }else{
  540.                     val<<=shift;
  541.                     mask<<=shift;
  542.                     break;
  543.                 }
  544.             }while(shift>0);
  545.             mask=~mask;
  546.             *ptr= *ptr & mask | (unsigned char)val;
  547.             ptr++;
  548.             shift=sizeof(*ptr)*8;
  549.         }while(n>0);
  550.         if(need32)SwapMMUMode((void *)&mode32);
  551.         break;
  552.     }
  553.     case 4:{
  554.         register unsigned char val,mask,*ptr=pixelPtr;
  555.         shift=sizeof(*ptr)*8;
  556.         shift-=bitsOffset;    // from right, instead of from left
  557.         if(need32)SwapMMUMode((void *)&mode32);
  558.         do{
  559.             val=mask=0;
  560.             do{
  561.                 shift-=4;
  562.                 val<<=4;
  563.                 mask<<=4;
  564.                 if(n>0){
  565.                     val|=(*value++)&15;
  566.                     mask|=15;
  567.                     n--;
  568.                 }else{
  569.                     val<<=shift;
  570.                     mask<<=shift;
  571.                     break;
  572.                 }
  573.             }while(shift>0);
  574.             mask=~mask;
  575.             *ptr= *ptr & mask | (unsigned char)val;
  576.             ptr++;
  577.             shift=sizeof(*ptr)*8;
  578.         }while(n>0);
  579.         if(need32)SwapMMUMode((void *)&mode32);
  580.         break;
  581.     }
  582.     case 8:{
  583.         register unsigned char *pB=pixelPtr;
  584.         if(need32)SwapMMUMode((void *)&mode32);
  585.         for(;n>0;n--) *pB++ = *value++;
  586.         if(need32)SwapMMUMode((void *)&mode32);
  587.         break;
  588.     }
  589.     case 16:{
  590.         register unsigned short *pW=(unsigned short *)pixelPtr;
  591.         if(need32)SwapMMUMode((void *)&mode32);
  592.         for(;n>0;n--) *pW++ = *value++;
  593.         if(need32)SwapMMUMode((void *)&mode32);
  594.         break;
  595.     }
  596.     case 32:{
  597.         register unsigned long *pL=(unsigned long *)pixelPtr;
  598.         if(need32)SwapMMUMode((void *)&mode32);
  599.         for(;n>0;n--) *pL++ = *value++;
  600.         if(need32)SwapMMUMode((void *)&mode32);
  601.         break;
  602.     }
  603.     }
  604. }
  605.  
  606. // Fast access to pixels, with no overhead
  607. void ReadPixels(int x,int y,int n,unsigned long *value
  608.     ,unsigned char *baseAddr,long pixelSize,long rowBytes)
  609. {
  610.     static Boolean can32,is32,firstTime=1;
  611.     Boolean need32;
  612.     signed char mode32=true32b;
  613.     short bitsOffset=0;
  614.     unsigned char *pixelPtr=baseAddr;
  615.     int shift;
  616.  
  617.     if(firstTime){
  618.         long gestalt;
  619.         can32=TrapAvailable(_SwapMMUMode);
  620.         Gestalt(gestaltAddressingModeAttr,&gestalt);
  621.         is32=gestalt&(1L<<gestalt32BitAddressing);
  622.         firstTime=0;
  623.     }
  624.     if(x!=0){
  625.             register long bits;
  626.  
  627.             bits=x*pixelSize;
  628.             pixelPtr+=bits>>3;
  629.             bitsOffset=bits&7;
  630.     }
  631.     if(y!=0)pixelPtr+=y*rowBytes;
  632.     need32=(unsigned long)pixelPtr>0xffffffUL;
  633.     if(need32 && !can32)PrintfExit("%s line %d: 32 bit address in 24 bit computer.\n"
  634.         ,__FILE__,__LINE__);
  635.     need32=need32 && !is32;
  636.     switch(pixelSize){
  637.     case 1:{
  638.         register unsigned char val,*ptr=pixelPtr;
  639.         shift=sizeof(*ptr)*8;
  640.         shift-=bitsOffset;    // from right, instead of from left
  641.         if(need32)SwapMMUMode((void *)&mode32);
  642.         do{
  643.             val=*ptr++;
  644.             do{
  645.                 shift-=1;
  646.                 if(n>0){
  647.                     *value++=(val>>shift)&1;
  648.                     n--;
  649.                 }else{
  650.                     break;
  651.                 }
  652.             }while(shift>0);
  653.             shift=sizeof(*ptr)*8;
  654.         }while(n>0);
  655.         if(need32)SwapMMUMode((void *)&mode32);
  656.         break;
  657.     }
  658.     case 2:{
  659.         register unsigned char val,*ptr=pixelPtr;
  660.         shift=sizeof(*ptr)*8;
  661.         shift-=bitsOffset;    // from right, instead of from left
  662.         if(need32)SwapMMUMode((void *)&mode32);
  663.         do{
  664.             val=*ptr++;
  665.             do{
  666.                 shift-=2;
  667.                 if(n>0){
  668.                     *value++=(val>>shift)&3;
  669.                     n--;
  670.                 }else{
  671.                     break;
  672.                 }
  673.             }while(shift>0);
  674.             shift=sizeof(*ptr)*8;
  675.         }while(n>0);
  676.         if(need32)SwapMMUMode((void *)&mode32);
  677.         break;
  678.     }
  679.     case 4:{
  680.         register unsigned char val,*ptr=pixelPtr;
  681.         shift=sizeof(*ptr)*8;
  682.         shift-=bitsOffset;    // from right, instead of from left
  683.         if(need32)SwapMMUMode((void *)&mode32);
  684.         do{
  685.             val=*ptr++;
  686.             do{
  687.                 shift-=4;
  688.                 if(n>0){
  689.                     *value++=(val>>shift)&15;
  690.                     n--;
  691.                 }else{
  692.                     break;
  693.                 }
  694.             }while(shift>0);
  695.             shift=sizeof(*ptr)*8;
  696.         }while(n>0);
  697.         if(need32)SwapMMUMode((void *)&mode32);
  698.         break;
  699.     }
  700.     case 8:{
  701.         register unsigned char *pB=pixelPtr;
  702.         if(need32)SwapMMUMode((void *)&mode32);
  703.         for(;n>0;n--) *value++=*pB++;
  704.         if(need32)SwapMMUMode((void *)&mode32);
  705.         break;
  706.     }
  707.     case 16:{
  708.         register unsigned short *pW=(unsigned short *)pixelPtr;
  709.         if(need32)SwapMMUMode((void *)&mode32);
  710.         for(;n>0;n--) *value++=*pW++;
  711.         if(need32)SwapMMUMode((void *)&mode32);
  712.         break;
  713.     }
  714.     case 32:{
  715.         register unsigned long *pL=(unsigned long *)pixelPtr;
  716.         if(need32)SwapMMUMode((void *)&mode32);
  717.         for(;n>0;n--) *value++=*pL++;
  718.         if(need32)SwapMMUMode((void *)&mode32);
  719.         break;
  720.     }
  721.     }
  722. }